import threading


def singleton(cls, *args, **kwargs):
    _instance = {}
    _lock = threading.Lock()

    def _inner():
        if cls not in _instance:
            with _lock:
                if cls not in _instance:
                    _instance[cls] = cls(*args, **kwargs)
        return _instance[cls]
    return _inner


@singleton
class Test:
    pass


test1 = Test()
test2 = Test()

assert test1 is test2, "不是单例"